<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>XOR (异或)训练</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="fann.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="ref.fann.html">Fann 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="fann.examples.html">范例</a></li>
    <li>XOR (异或)训练</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="fann.examples-1" class="section">
  <h2 class="title">XOR (异或)训练</h2>
  <p class="para">
   以下例子展示了怎么训练数据来实现 XOR (异或)功能。
   <div class="example" id="example-3399">
    <p><strong>示例 #1 xor.data file</strong></p>
    <div class="example-contents">
<div class="txtcode"><pre class="txtcode">4 2 1
-1 -1
-1
-1 1
1
1 -1
1
1 1
-1</pre>
</div>
    </div>

   </div>
   <div class="example" id="example-3400">
    <p><strong>示例 #2 一般训练</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$num_input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$num_output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$num_layers&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$num_neurons_hidden&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$desired_error&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0.001</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$max_epochs&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">500000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$epochs_between_reports&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$ann&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fann_create_standard</span><span style="color: #007700">(</span><span style="color: #0000BB">$num_layers</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num_input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num_neurons_hidden</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$num_output</span><span style="color: #007700">);<br /><br />if&nbsp;(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fann_set_activation_function_hidden</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FANN_SIGMOID_SYMMETRIC</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fann_set_activation_function_output</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FANN_SIGMOID_SYMMETRIC</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$filename&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"/xor.data"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">fann_train_on_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$filename</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$max_epochs</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$epochs_between_reports</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$desired_error</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fann_save</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"/xor_float.net"</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fann_destroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   这个例子展示怎么读取神经网络并且使用 XOR (异或)功能来运行数据。
   <div class="example" id="example-3401">
    <p><strong>示例 #3 一般测试</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$train_file&nbsp;</span><span style="color: #007700">=&nbsp;(</span><span style="color: #0000BB">dirname</span><span style="color: #007700">(</span><span style="color: #0000BB">__FILE__</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"/xor_float.net"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">is_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$train_file</span><span style="color: #007700">))<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"The&nbsp;file&nbsp;xor_float.net&nbsp;has&nbsp;not&nbsp;been&nbsp;created!&nbsp;Please&nbsp;run&nbsp;simple_train.php&nbsp;to&nbsp;generate&nbsp;it"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$ann&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fann_create_from_file</span><span style="color: #007700">(</span><span style="color: #0000BB">$train_file</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$ann</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"ANN&nbsp;could&nbsp;not&nbsp;be&nbsp;created"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;array(-</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$calc_out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fann_run</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"xor&nbsp;test&nbsp;(%f,%f)&nbsp;-&gt;&nbsp;%f\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #0000BB">$calc_out</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">fann_destroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$ann</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div></div></div></body></html>